package com.senno.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.senno.entity.Employee;
import com.senno.entity.UserInfo;
import com.senno.service.EmployeeService;
import com.senno.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.UUID;

@Controller
@RequestMapping("/")
public class LoginController {

//    @Autowired
//    private UserInfoService userInfoService;

    //注解获取employeeService的实现类对象
    @Autowired
    private EmployeeService employeeService;

    //跳转登录页
    @RequestMapping("login")
    public String toLogin(){
        return "login";
    }

    //登录验证
    @RequestMapping("checkLogin")
    public String checkLogin(Model model, HttpSession session){
        //根据security验证情况 获取登录名
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String employeeName = authentication.getName();
        //若employeeName的值为anonymousUser 则没有通过验证 返回登录页面
        if("anonymousUser".equals(employeeName)){
            return "login";
        }
//        UserInfo userInfo = userInfoService.selectByUserName(username);
        //验证通过 查询出员工信息 存入到session中 跳转到主页
        Employee employee = employeeService.selectEmployeeByName(employeeName);
        if (employee != null) {
            session.setAttribute("loginInfo", employee);
            model.addAttribute("userInfo",employee);
            return "pages/main";
        }else{
            //这种情况一般出现不了， 但还是写在这里 做个保险
            //查询不到员工信息 进行跳转到登录页
            return "login";
        }
    }

    //跳转到欢迎页
    @RequestMapping("welcome")
    public String toWelcome(){
        return "welcome";
    }

}
